class IndexController < ApplicationController
  caches_page :index,:method_missing
  
  def index
    @user = User.count
    @company = Company.count
    @incumbent = Incumbent.count
    @keywords = Keyword::SearchWord.find(:all,:order => "search_times DESC",:limit => 5)
  end
  
  def method_missing(name, *args) 
    render :template=>"index/null"
  end
  
  def post_tag_notice
    render :inline=>%{&nbsp;<script type="text/javascript">openAlert('标签可以标记你提供的信息，其他用户可以通过标签方便地找到这些信息。',400,50);</script>}
  end
  
  def post_picture
    picture = Picture.find(params[:id])
    send_data(picture.data,
              :filename=>picture.name,
              :type =>picture.content_type,
              :disposition=>"inline")
  end
  
  def post_validation
    picture=Validation.new(params[:filename])
    session[:code]=picture.code
    send_data(picture.data,
              :filename=>picture.filename,
              :type =>picture.type,
              :disposition=>"inline")
  end
  
  def post_position
    if request.raw_post.to_i.integer? && request.raw_post.to_i>0
      @positions = Position.find(:all,:conditions=>["function_id=?",request.raw_post]).map {|u| [u.name, u.id] }
      render :inline=>%{<%= select 'incumbent', 'tposition_id',@positions%>}
    else
      render :text=>"请选择所属职能"
    end
  end
  
  
  def post_city
    if request.raw_post.to_i.integer? && request.raw_post.to_i>0
      @cities = City.find(:all,:conditions=>["region_id=?",request.raw_post]).map {|u| [u.name, u.id] }
      render :inline=>%{<%= select 'incumbent', 'tcity_id',@cities%>}
    else
      render :text=>"请选择所在地区"
    end
  end
  
  def post_rank
    if request.raw_post.to_i.integer? && request.raw_post.to_i>0
      @i = request.raw_post.to_i
      render :inline=>%{<%= select 'incumbent', 'trank',1..@i%>}
    else
      render :text=>"请输入一个有效的所在公司的内部级别。（整数型，数字越大，级别越高）"
    end
  end
  
  def post_company_rank
    if request.raw_post.to_i.integer? && request.raw_post.to_i>0
      id = request.raw_post.to_i
      @company=Company.find_by_id(id)
      render :inline=>%{<%= select 'incumbent', 'trank',1..@company.grade%>}
    else
      render :text=>"请输入一个有效的所在公司的内部级别。（整数型，数字越大，级别越高）"
    end
  end
  
  def post_company_info
    id = request.raw_post.to_i
    if id==0
      render :partial=>"report/create_company"
    else
      @company=Company.find_by_id(id)
      render :partial=>"report/company_info"
    end
  end
  
  def post_company
    company_name = params[:company_name].strip
    additional_conditions="industry_id ="+ params[:company_tindustry_id] + " and entity_id ="+params[:company_tentity_id]+" and deleted=0"
    query_conditions=Keyword.create_ajax_conditions(company_name,3,additional_conditions)      
    @companies=Company.find(:all,:conditions =>query_conditions,:order => "view_times DESC",:limit => 4)
    render :partial => "/report/companies_list"
  end
  
  def listposition
    id = request.raw_post.to_i
    @positions=Position.find(:all,:conditions=>["function_id=?",id]).map {|u| [u.name, u.id] }
    render :inline=>%{<%= select 'position', 'id',@positions,{},{:size =>"7",:style=>"width:175px;"}%>}
  end
  
  def listcompany
    id = request.raw_post.to_i
    @companies=Company.find(:all,:conditions=>["industry_id=?",id]).map {|u| [u.name, u.id] }
    render :inline=>%{<%= select 'company', 'id',@companies,{},{:size =>"7",:style=>"width:175px;"}%>}
  end
  
  def listcity
    id = request.raw_post.to_i
    @cities=City.find(:all,:conditions=>["region_id=?",id]).map {|u| [u.name, u.id] }
    render :inline=>%{<%= select 'city', 'id',@cities,{},{:size =>"7",:style=>"width:175px;"}%>}
  end
  
end
